#include "Camera.h"
#include "Game.h"
#include "GamePlayState.h"
#include "GameObject.h"

Camera* Camera::instance = nullptr;

Camera* Camera::GetInstance(void)
{
	if(instance == nullptr)
		instance = new Camera();
	return instance;
}

void Camera::DeleteInstance(void)
{
	delete instance;
	instance = nullptr;
}

// Constructor
Camera::Camera(void)
{
	offsetX = 0;
	offsetY = 0;
	scrollSpeed = 100.0f;
	screenWidth = Game::GetInstance()->GetScreenWidth();
	screenHeight = Game::GetInstance()->GetScreenHeight();
	isScrolling = false;

	player = nullptr;
	exitDoor = nullptr;
}

// Destructor
Camera::~Camera(void)
{
}

// Returns the camera's X offset.
float Camera::OffsetX(void) const
{
	return offsetX;
}

// Returns the camera's Y offset.
float Camera::OffsetY(void) const
{
	return offsetY;
}

// IsScrolling
//	- RETURNS TRUE if the camera is currently scrolling through
//	  the level.
//	- RETURNS FALSE if the camera is following the player.
bool Camera::IsScrolling(void)
{
	return isScrolling;
}

void Camera::SetPlayer(GameObject* player)
{
	this->player = player;
}

void Camera::SetExitDoor(GameObject* door)
{
	this->exitDoor = door;
}

void Camera::SetWorldWidth(int width)
{
	worldWidth = width;
}

void Camera::SetWorldHeight(int height)
{
	worldHeight = height;
}

// SkipScrolling
//	- Forces the camera to stop scrolling and follow the player.
void Camera::SkipScrolling(void)
{
}

// Updates the camera.
void Camera::Update(float deltaTime)
{
	if(player != nullptr)
	{
		this->offsetX = player->GetPosX() - (screenWidth * 0.5f);
		this->offsetY = player->GetPosY() - (screenHeight * 0.5f);

		if(offsetX < 0.0f)
			offsetX = 0.0f;
		else if(offsetX + screenWidth > worldWidth)
			offsetX = (float)(worldWidth - screenWidth);

		if(offsetY < 0.0f)
			offsetY = 0.0f;
		else if(offsetY + screenHeight > worldHeight)
			offsetY = (float)(worldHeight - screenHeight);
	}
}

